package intro.databases.gtCarRental.ui.employee;

import intro.databases.gtCarRental.GTCRActivity;
import intro.databases.gtCarRental.R;
import intro.databases.gtCarRental.classes.Car;
import intro.databases.gtCarRental.classes.Location;

import java.util.ArrayList;

import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class ManageCars_AddCar extends GTCRActivity {
	EditText vehicleSNO, carModel, color, hourlyRate, dailyRate, seatingCapacity;
	Spinner carType, location, transmissionType, bluetoothConnectivity, auxillaryCable;
	
	@Override
	public void onCreate(Bundle b) {
		super.onCreate(b);
		setContentView(R.layout.add_cars);
		setupViews();
	}
	
	private void setupViews() {
		vehicleSNO = (EditText) findViewById(R.id.editText_manageCars_addCars_vehicleSNO);
		carModel = (EditText) findViewById(R.id.editText_manageCars_addCars_carModel);
		color = (EditText) findViewById(R.id.editText_manageCars_addCars_color);
		hourlyRate = (EditText) findViewById(R.id.editText_manageCars_addCars_hourlyRate);
		dailyRate = (EditText) findViewById(R.id.editText_manageCars_addCars_dailyRate);
		seatingCapacity = (EditText) findViewById(R.id.editText_manageCars_addCars_seatingCapacity);

		carType = (Spinner) findViewById(R.id.spinner_manageCars_addCars_carType);
		location = (Spinner) findViewById(R.id.spinner_manageCars_addCars_location);
		transmissionType = (Spinner) findViewById(R.id.spinner_manageCars_addCars_transmissionType);
		bluetoothConnectivity = (Spinner) findViewById(R.id.spinner_manageCars_addCars_bluetoothConnectivity);
		auxillaryCable = (Spinner) findViewById(R.id.spinner_manageCars_addCars_auxillaryCable);

		ArrayAdapter<CharSequence> ctAdapter = ArrayAdapter.createFromResource(this, R.array.gtcr_type, android.R.layout.simple_spinner_item);
		ctAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		carType.setAdapter(ctAdapter);
		
		ArrayAdapter<String> lcAdapter = getLocationAdapter();//ArrayAdapter.createFromResource(this, R.array.gtcr_userTypes, android.R.layout.simple_spinner_item);
		lcAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		location.setAdapter(lcAdapter);
		
		ArrayAdapter<CharSequence> tsmtAdapter = ArrayAdapter.createFromResource(this, R.array.transmissionType, android.R.layout.simple_spinner_item);
		tsmtAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		transmissionType.setAdapter(tsmtAdapter);
		
		ArrayAdapter<CharSequence> btAdapter = ArrayAdapter.createFromResource(this, R.array.yes_or_no, android.R.layout.simple_spinner_item);
		btAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		bluetoothConnectivity.setAdapter(btAdapter);
		
		ArrayAdapter<CharSequence> auxAdapter = ArrayAdapter.createFromResource(this, R.array.yes_or_no, android.R.layout.simple_spinner_item);
		auxAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		auxillaryCable.setAdapter(auxAdapter);
	}

	private ArrayAdapter<String> getLocationAdapter() {
		ArrayList<Location> locs = dbConn.getAllLocations();
		ArrayList<String> locNames = new ArrayList<String>();
		for(Location loc : locs)
			locNames.add(loc.locationName);
		
		ArrayAdapter<String> retVal = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,locNames);		
		return retVal;		
	}

	public void addCar(View v) {
		Car car = new Car();
		car.vehicleSNO = Integer.parseInt(vehicleSNO.getText().toString());
		car.auxillaryCable = (((TextView) auxillaryCable.getSelectedView()).getText().toString()).equals("Yes");
		car.transmissionType = ((TextView) transmissionType.getSelectedView()).getText().toString();
		car.seatingCapacity = Integer.parseInt(seatingCapacity.getText().toString());
		car.bluetoothConnectivity = (((TextView) bluetoothConnectivity.getSelectedView()).getText().toString()).equals("Yes");
		car.dailyRate = Integer.parseInt(dailyRate.getText().toString());
		car.hourlyRate = Integer.parseInt(hourlyRate.getText().toString());
		car.color = color.getText().toString();
		car.type = ((TextView) carType.getSelectedView()).getText().toString();
		car.carModel = carModel.getText().toString();
		car.underMaintenanceFlag = false; //new car assumed to be not under maintenance
		car.locationName = ((TextView) location.getSelectedView()).getText().toString();
		
		dbConn.insertCar(car);
		Toast.makeText(this, "Successfully added "+car.carModel+"("+car.vehicleSNO+") at "+car.locationName, Toast.LENGTH_LONG).show();
		this.finish();
	}
}
